package com.bruce.controller;

import com.bruce.pojo.User;
import com.bruce.service.UserService;
import com.springmvc.anotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class UserController {

    @AutoWired
    UserService userService;

    @RequestMapping("/user/query")
    public void findUsers(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String username){
        response.setContentType("text/html;charset=utf-8");
        try{
            List<User> user = userService.findUser(username);
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<h1>SpringMvc控制器：" + username + "</h1>");
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/user/jsp")
    public String jsp(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        request.setAttribute("msg", "哈哈哈哈");
        return "forward:/user.jsp";
    }

    @RequestMapping("/user/body")
    @ResponseBody
    public List<User> userList(HttpServletRequest request, HttpServletResponse response){
        return userService.findUser("123");
    }
}
